package com.bw.hxc.utils;

import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;

import java.util.HashMap;
import java.util.Map;

public class SmsUtils {

     public static void sendSms(String tel,String code) throws Exception {
         // 用短信发送出去
         String apiUrl = "https://sms_developer.zhenzikj.com";
         String appId = "111546";
         String appSecret = "02bf457e-f330-4e66-aa7e-c11c78d37f1a";

         ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);

         Map<String, Object> params = new HashMap<String, Object>();
         params.put("number",tel);//  手机号码

         params.put("templateId", "9238");// 模板
         String[] templateParams = new String[2];
         templateParams[0] = code;
         templateParams[1] = "1";
         params.put("templateParams", templateParams);
         //发送验证码
         System.err.println("验证码是："+code);
         client.send(params);
     }


    /**
     * 获取用户认证信息
     * @param usernmae
     * @param password
     * @return
     */
    public static Authentication getUserInfo(AuthenticationManager  authenticationManager, String usernmae, String password) {

        UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(usernmae, password);
        try {
            // 验证用户名和密码是否正确  如果不正确会抛出异常
            Authentication authentication = authenticationManager.authenticate(upToken);
            //    // 获取登录用户名信息
            //   User loginUser = (User) authentication.getPrincipal();
            return authentication;
        } catch (Exception e) {
            System.err.println(e);
            return null;
        }
    }
}
